R

library(plotly)
Loading required package: ggplot2

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2
──
✔ tibble  3.1.8     ✔ dplyr   1.1.0
✔ tidyr   1.3.0     ✔ stringr 1.5.0
✔ readr   2.1.4     ✔ forcats 1.0.0
✔ purrr   1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks plotly::filter(), stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(ggplot2)
library(tidyr)
library(htmlwidgets)
library(dplyr)
options(warn = - 1)
library(withr)
library(scales)

Attaching package: 'scales'

The following object is masked from 'package:purrr':

    discard

The following object is masked from 'package:readr':

    col_factor
# lubridate, zoo, xts
options(scipen=999)
library(reshape2)

Attaching package: 'reshape2'

The following object is masked from 'package:tidyr':

    smiths
jobs_df <- read_csv('../../data/job_data.csv')
New names:
Rows: 823 Columns: 17
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," chr
(13): title, company_name, location, via, description, schedule_type, qu... dbl
(3): ...1, salary, experience lgl (1): remote
ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
Specify the column types or set `show_col_types = FALSE` to quiet this message.
• `` -> `...1`
head(jobs_df)
# A tibble: 6 × 17
   ...1 title compa…¹ locat…² via   descr…³ sched…⁴ salary query quali…⁵ respo…⁶
  <dbl> <chr> <chr>   <chr>   <chr> <chr>   <chr>    <dbl> <chr> <chr>   <chr>  
1     0 Ethe… Ex Pop… Anywhe… Buil… "Compa… Full-t…     NA Bloc… "['2-3… ['Desi…
2     1 Bloc… 21.co   New Yo… Gree… "We ar… Full-t… 180000 Bloc… "[\"Ba… ['As a…
3     2 Bloc… Blockc… Anywhe… Link… "Are y… Contra…     NA Bloc… "['3+ … ['Our …
4     3 Pyth… Upwork  Anywhe… Upwo… "Need … Contra…  41600 Bloc… "['Can… ['Will…
5     4 Bloc… Telnyx  United… Star… "About… Full-t…     NA Bloc… "['You… ['To b…
6     5 Ethe… Turnbl… Anywhe… ZipR… "Our c… Full-t… 150000 Bloc… "['5+ … ['As a…
# … with 6 more variables: benefits <chr>, degree <chr>, experience <dbl>,
#   remote <lgl>, city <chr>, state <chr>, and abbreviated variable names
#   ¹​company_name, ²​location, ³​description, ⁴​schedule_type, ⁵​qualifications,
#   ⁶​responsibilities
# create a new column 'experience_group' based on 'experience'
jobs_df$experience_group <- cut(jobs_df$experience, c(0, 5, 10, Inf),
                                 labels = c('0-5', '5-10', '10+'))
jobs_df_pivot <- jobs_df %>%
  group_by(degree, experience_group) %>%
  summarise(mean_salary = mean(salary, na.rm = TRUE)) %>%
  pivot_wider(names_from = experience_group, values_from = mean_salary)
`summarise()` has grouped output by 'degree'. You can override using the
`.groups` argument.
jobs_df_pivot <- jobs_df_pivot[1:(nrow(jobs_df_pivot)-1), 1:(ncol(jobs_df_pivot)-1)]
# Create the plotly plot
ap <- jobs_df_pivot %>% plot_ly(
    hovertext = "Degree, Salary"
)  

ap <- ap %>%
  add_trace(x = jobs_df_pivot$degree, y = ~`0-5`, type = 'bar', name = '0-5', marker = list(color = '#118C4F'))

ap <- ap %>%
  add_trace(x = jobs_df_pivot$degree, y = ~`5-10`, type = 'bar', name = '5-10', marker = list(color = '#FFB90D'))

ap <- ap %>%
  add_trace(x = jobs_df_pivot$degree, y = ~`10+`, type = 'bar', name = '10+' , marker = list(color = 'red'))

# Set the plot layout and theme
ap <- ap %>% 
  layout(
    title = list(text = "", y = 0.98, font = list(family = "Arial", size = 18, color = "white")),
    xaxis = list(title = list(text = "Degree", font = list(family = "Arial", color = "white")), 
                 tickfont = list(color = "white")),
    yaxis = list(title = list(text = "Salary (USD)", font = list(family = "Arial", color = "white")),
                 tickprefix = "$", tickformat = ",",
                 tickfont = list(color = "white")),
    legend = list(title = list(text = "Years of Experience", font = list(color = "white")), 
                  font = list(color = "white")),
    template = "plotly_dark",
    paper_bgcolor = "black",
    plot_bgcolor = "black"
  )


# Show the plot
ap
saveWidget(ap, file = "../../website/plots/plot-10.html")
# Calculate the count of remote and non-remote jobs
remote_count <- c(sum(jobs_df$remote == TRUE), sum(jobs_df$remote == FALSE))

# Create a pie chart with Plotly
pc <- plot_ly(labels = c("Remote", "Non-Remote"), values = remote_count, type = "pie",
        textinfo = "value+percent", hole = 0.6,
        marker = list(colors = c("#00CC96", "#EF553B"))) %>%
  layout(font = list(family = "Arial", color = "white"),
         textfont = list(color = "white"),
         paper_bgcolor = "black",
        plot_bgcolor = "black")

pc 
saveWidget(pc, file = "../../website/plots/plot-15.html")